home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: thinkage.on.ca!atbowler
- From: atbowler@thinkage.on.ca (Alan Bowler)
- Subject: Re: saving system()'s output to a C variable?
- Message-ID: <Dq2uBo.5vE@thinkage.on.ca>
- Sender: news@thinkage.on.ca
- Organization: Thinkage Ltd.
- References: <96104.233554U61441@uicvm.uic.edu> <smryanDpuDMI.Cyq@netcom.com>
- Date: Thu, 18 Apr 1996 21:28:35 GMT
-
- In article <smryanDpuDMI.Cyq@netcom.com> smryan@netcom.com (@#$%!?!) writes:
- >: Is it possible to save the output of the system() command to
- >: a C variable? For example, when I have the line system("date")
- >: in my C program, I want the output of this command to go to a
- >: C variable (e.g. char array) instead of going on the stdout.
- >: Please send me email at swaheed@uic.edu
- >
- >If your system has pipes or the like, you can use popen/pclose.
- >Otherwise, just the save the output to a file and then read the
- >file.
- >
- > system("date >xx");
- > f = fopen("xx","r");
- > fgets(array,size,f);
- > fclose(f);
-
- This is likely to be the most widely portable technique.
- Rather than clobbing the user's file "xx", it would be
- a better idea to use tmpnam(), and arrange to delete
- the file when you are done.
-